package patrol.mobile.com.mobilepatrol.activity.monitor;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.adapter.ImageAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.ImageBean2;
import patrol.mobile.com.mobilepatrol.bean.JiankkElefailrecEntity;
import patrol.mobile.com.mobilepatrol.utils.DatePickerUtil02;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack2;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;

/**
 * Created by z on 2018/9/20.
 * Des: 故障内容
 */

public class GuzhangNeiRongActivity extends BaseActivity {
    @BindView(R.id.mEditBaozhang)
    TextView mEditBaozhang;
    @BindView(R.id.mEditbaozhangren)
    TextView mEditbaozhangren;
    @BindView(R.id.mEditbaozhangTime)
    TextView mEditbaozhangTime;
    @BindView(R.id.mSpinnerSblb)
    TextView mSpinnerSblb;
    @BindView(R.id.mEditBzms)
    TextView mEditBzms;
    @BindView(R.id.mEditSbmc)
    TextView mEditSbmc;
    @BindView(R.id.mSpinnerGzyy)
    TextView mSpinnerGzyy;
    @BindView(R.id.mEditXfsx)
    TextView mEditXfsx;
    @BindView(R.id.mEditWxDaw)
    TextView mEditWxDaw;
    @BindView(R.id.mEditTzdwwxsj)
    TextView mEditTzdwwxsj;
    @BindView(R.id.textXfJu)
    TextView textXfJu;
    @BindView(R.id.mSpinnerYy)
    TextView mSpinnerYy;
    @BindView(R.id.mSpinnerWxFs)
    TextView mSpinnerWxFs;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.editDqsj)
    TextView editDqsj;

    // elefailurerpt;        // elefailurerpt.failureType 保障类型  1:收费 2:通信 3:供电 4:照明 5:通风 6:监控  elefailurerpt.date 报障时间
    //maintenance;        // 维修方式 1 更换,2 线路,3 调试,4 其它
    // failureReason;        // 故障原因 1 线路,2 供电,3 硬件,4 软件
    //repairFlag;        // 维修状态 1：未维修  2：维修中 3：维修完成
    private String id;
    private JiankkElefailrecEntity jiankkElefailrecEntity;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_guzhangneirong;
    }

    @Override
    public void initTitle() {
        super.initTitle();
        id = getIntent().getStringExtra("id");
        if (!TextUtils.isEmpty(id)) {
            Log.e("!!!!!!", id);
            getHttpData();
        }
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("故障原因");
        editDqsj.setOnClickListener(view ->
                DatePickerUtil02.getInstance(view.getContext()).setCallBack((DateBean dateBean) ->
                        editDqsj.setText(dateBean.getmDate())
                ));
    }

    private void getHttpData() {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("id", id);
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getJiankkElefailrecEntity);
        sender.requestPost(hashMap, new HttpCallBack2(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response, String attachList) {
                jiankkElefailrecEntity = new Gson().fromJson(response, JiankkElefailrecEntity.class);
                mEditBaozhang.setText(jiankkElefailrecEntity.getOrg().getOrgName());
                mEditbaozhangren.setText(jiankkElefailrecEntity.getAccount().getName());
                mEditbaozhangTime.setText(jiankkElefailrecEntity.getDate());
                switch (jiankkElefailrecEntity.getFailureType()) {
                    case "1":
                        mSpinnerSblb.setText("收费");
                        break;
                    case "2":
                        mSpinnerSblb.setText("通信");
                        break;
                    case "3":
                        mSpinnerSblb.setText("供电");
                        break;
                    case "4":
                        mSpinnerSblb.setText("照明");
                        break;
                    case "5":
                        mSpinnerSblb.setText("通风");
                        break;
                    case "6":
                        mSpinnerSblb.setText("监控");
                        break;
                }
                switch (jiankkElefailrecEntity.getFailureReason()) {
                    case "1":
                        mSpinnerYy.setText("线路");
                        break;
                    case "2":
                        mSpinnerYy.setText("供电");
                        break;
                    case "3":
                        mSpinnerYy.setText("硬件");
                        break;
                    case "4":
                        mSpinnerYy.setText("软件");
                        break;
                }
                mEditBzms.setText(jiankkElefailrecEntity.getFailureContent());
                mEditXfsx.setText(jiankkElefailrecEntity.getFinishDate());//修复时限
                mEditWxDaw.setText(jiankkElefailrecEntity.getOrg().getOrgName());
                mEditTzdwwxsj.setText(jiankkElefailrecEntity.getDate());
                mEditSbmc.setText(jiankkElefailrecEntity.getElefailurerpt().getEquipmentName());
                if (attachList.length() > 10) {
                    recyclerView.setVisibility(View.VISIBLE);
                    List<ImageBean2> imageBeans = GsonUtil.jsonToList(attachList, ImageBean2.class);
                    Log.e("________", new Gson().toJson(imageBeans));
                    List<String> list = new ArrayList<>();
                    for (ImageBean2 imageBean : imageBeans) {
                        list.add((baseUrl.substring(0, baseUrl.length() - 1) + imageBean.getFilePath()).replace("\\", "/"));
                    }
                    recyclerView.setLayoutManager(new GridLayoutManager(GuzhangNeiRongActivity.this, 4));
                    recyclerView.setAdapter(new ImageAdapter(list));
                }
            }
        });
    }

    @Override
    public void setListener() {
        textXfJu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GuzhangNeiRongActivity.this, WeiXiuJiLuActivity.class);
                intent.putExtra("id", jiankkElefailrecEntity.getId());
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
